package view.library
{
	import events.library.EventAddResourcesToLibrary;
	import events.library.EventRemoveLibraryItems;
	import events.main.EventBrowseForFiles;
	
	import flash.desktop.ClipboardFormats;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.NativeDragEvent;
	import flash.filesystem.File;
	
	import model.ModelBrowseFile;
	import model.ModelLibrary;
	
	import mx.controls.Alert;
	import mx.events.CloseEvent;
	import mx.managers.DragManager;
	
	import org.flashIso.engine.library.objects.LBinaryData;
	import org.robotlegs.mvcs.Mediator;
	
	import spark.events.IndexChangeEvent;
	
	public class MediatorScreenBinaries extends Mediator
	{
		[Inject]
		public var v:ScreenBinaries;
		
		[Inject]
		public var mb:ModelBrowseFile;
		
		[Inject]
		public var ml:ModelLibrary;
		
		override public function onRegister() : void  {
			v.list.dataProvider = ml.binaryDataProvider;

			eventMap.mapListener(v.btnAdd, MouseEvent.CLICK, btnAddPress, MouseEvent);
			eventMap.mapListener(v.list, IndexChangeEvent.CHANGE, listSelectionChange, IndexChangeEvent);
			eventMap.mapListener(v.btnRemove, MouseEvent.CLICK, btnRemoveObject, MouseEvent);
			eventMap.mapListener(v.btnUpdate, MouseEvent.CLICK, btnUpdateHandler, MouseEvent);
			
			eventMap.mapListener(v.list, NativeDragEvent.NATIVE_DRAG_ENTER, dragEnter);
			eventMap.mapListener(v.list, NativeDragEvent.NATIVE_DRAG_DROP, dragDrop);
		}
		
		private function dragEnter(e:NativeDragEvent) : void {
			DragManager.acceptDragDrop(v.list);
		}
		private function dragDrop(e:NativeDragEvent) : void {
			var files:Array = e.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
			var paths:Array = new Array();
			var i:uint;
			for (i = 0; i < files.length; i++){
				paths.push(File(files[i]).nativePath);
			}
			dispatch(new EventAddResourcesToLibrary(EventAddResourcesToLibrary.ADD_RESOURCE_TO_LIBRARY, null, paths));
		}
		public function btnUpdateHandler(e:MouseEvent) : void {
			dispatch(new EventBrowseForFiles(EventBrowseForFiles.BROWSE_FOR_FILES, mb.BROWSE_FOR_UPDATE_BINARY, null, LBinaryData(v.list.selectedItem).id));
		}
		
		
		public function btnAddPress(e:MouseEvent) : void {
			dispatch(new EventBrowseForFiles(EventBrowseForFiles.BROWSE_FOR_FILES, mb.BROWSE_FOR_OPEN_BINARIES));
		}
		
		public function listSelectionChange(e:IndexChangeEvent) : void {
			v.btnUpdate.enabled = (v.list.selectedItems.length == 1);
			if (v.list.selectedItem != null && v.list.selectedIndices.length == 1) {
				v.textArea.text = LBinaryData(v.list.selectedItem).data.toString().substr(0,30000);
				LBinaryData(v.list.selectedItem).addEventListener(Event.CHANGE, binaryChangeEvent, false, 0, true);
			}else{
				v.textArea.text = "";
			}
		}
		//when binary updates update also the preview
		private function binaryChangeEvent(e:Event) : void {
			if (v.list.selectedItem != null && v.list.selectedIndices.length == 1) {
				v.textArea.text = LBinaryData(v.list.selectedItem).data.toString().substr(0,30000);
			}else{
				v.textArea.text = "";
			}
		}
		
		private function btnRemoveObject(e:MouseEvent) : void {
			Alert.show("Are you sure you want to delete selected binaries?","Delete Binaries", Alert.YES | Alert.NO, null, removeSelectedObjects);
		}
		private function removeSelectedObjects(e:CloseEvent) : void {
			if ((e.detail & Alert.YES) > 0){
				if (v.list.selectedItem != null) {
					dispatch(new EventRemoveLibraryItems(EventRemoveLibraryItems.REMOVE_LIBRARY_ITEM, v.list.selectedItems));
				}
			}
		}
		
	}
}